<?php

use app\models\AsrAudioClassify;
use yii\helpers\Html;
use yii\widgets\ActiveForm;

$this->title = '';
$lc_id = 0;
$parent_id = 0;
if (!empty($lc)){
    //业务流程的id
    $lc_id= $lc->id;
    //业务流程的父节点的id
    $parent_id= $lc->parent_id;
}
?>

<link href="../assets/public/css/asr-audio/asr-audio.min.css" rel="stylesheet">

<link href="../assets/public/css/asr-audio/main.css" rel="stylesheet">

<script src="../assets/public/js/vue.min.js"></script>

<script src="../assets/public/js/asr-audio/asr-audio.min.js"></script>

<script src="../assets/public/js/pekeUpload.min.js"></script>

<!--    话术流程导航   -->
<header class="main-header" style="z-index:1; margin: -30px -15px;">
    <!--    话术流程导航   -->
    <nav class="navbar navbar-default " style="margin-left: 0px;background-color: #0071a6">
        <div class="collapse navbar-collapse pull-left" id="navbar-collapse">
            <ul class="nav nav-pills nav-justified">
                <a style="color:white; margin: 15px;" id="back" class="pull-left"
                   href="index?page=<?php echo isset($_GET['page']) ? $_GET['page'] : 0; ?>&per-page=<?php echo isset($_GET['per-page']) ? $_GET['per-page'] : 0; ?>"><i
                            class="fa fa-backward"></i> 返回</a>
                <li class="dropdown" id="main-system"><a
                            href="update?id=<?php echo $id ?>&page=<?php echo isset($_GET['page']) ? $_GET['page'] : 0; ?>&per-page=<?php echo isset($_GET['per-page']) ? $_GET['per-page'] : 0; ?>"
                            class="dropdown-toggle" data-toggle="dropdown">主流程</a>
                </li>
                <li class="dropdown active" id="profession">
                    <a href="profession?id=<?php echo $id ?>&page=<?php echo isset($_GET['page']) ? $_GET['page'] : 0; ?>&per-page=<?php echo isset($_GET['per-page']) ? $_GET['per-page'] : 0; ?>"
                       class="dropdown-toggle" data-toggle="dropdown" id="profession">业务流程</a>
                </li>
                <li class="dropdown" id="system-flow">
                    <a href="system?id=<?php echo $id ?>&page=<?php echo isset($_GET['page']) ? $_GET['page'] : 0; ?>&per-page=<?php echo isset($_GET['per-page']) ? $_GET['per-page'] : 0; ?>"
                       class="dropdown-toggle" data-toggle="dropdown" id="system">系统流程</a>
                </li>
                <li class="dropdown" id="stop">
                    <a href="end?id=<?php echo $id ?>&page=<?php echo isset($_GET['page']) ? $_GET['page'] : 0; ?>&per-page=<?php echo isset($_GET['per-page']) ? $_GET['per-page'] : 0; ?>"
                       class="dropdown-toggle" data-toggle="dropdown">结束</a>
                </li>
            </ul>
        </div>
    </nav>

</header>

<!--內容-->
<div class="frame-body-box">

    <div class="frame-right-box">

        <div id="answerDetail" class="" style="display: block;margin-top: 45px">

            <div class="answerContent">

                <!--內容內的頭-->
                <div class="title">
                    <h4>话术设置</h4>
                </div>

                <!--导航栏下面的所有的数据-->
                <div class="answerMain">
                    <!--左边的显示节点列表数据-->
                    <div class="ansLeft">
                        <div class="sonNav">
                            <div class="sonList">
                                <?php
                                $kk = 0;
                                foreach ($nodeList as $k => $v) {

                                    if ($v['level'] == 1) {
                                        echo '</ul>';
                                        if ($v['id']==$parent_id){
                                            echo '<ul class="son2 scene' . $kk . ' open">';
                                        }else{
                                            echo '<ul class="son2 scene' . $kk . '">';
                                        }
                                        echo '<li class="businessTit"><p>' . $v['name'] . '<em class="triangle"></em></p></li>';
                                        $kk++;
                                    } else {
                                        if ($v['id']==$lc_id){
                                            echo '<li parent_id="' . $v['parent_id'] . '" id="' . $v['id'] . '" style="color:#4fa2fe" ><div class="sTit"><p class="">' . $v['name'] . '</p> <span title="添加" class="hBox"><img
                                                        src="../assets/public/img/asr-audio/answer_add.png"></span></div></li>';
                                        }else{
                                            echo '<li parent_id="' . $v['parent_id'] . '" id="' . $v['id'] . '" ><div class="sTit"><p class="">' . $v['name'] . '</p> <span title="添加" class="hBox"><img
                                                        src="../assets/public/img/asr-audio/answer_add.png"></span></div></li>';
                                        }
                                    }
                                }
                                ?>
                            </div>

                            <p class="sonAdd" id="add_parent">+ <span>添加</span></p></div>
                    </div>

                    <!--右边的显示数据-->
                    <div class="ansRight">
                        <div class="flowMain flow">

                            <!--话术节点名称,命中该标签客户意向分,所属语境,该节点当前是属于哪个语境-->
                            <div class="mBox hiddenToVisible">

                                <div class="fmBox">
                                    <div class="main-title">
                                        <span class="title-left">话术节点名称：</span>
                                        <div class="el-input">
                                            <input id="name" type="text" autocomplete="off" maxlength="6" class="el-input__inner">
                                        </div>
                                    </div>
                                    <div class="flowScore">
                                        <span class="title-left">命中该标签客户意向分
                                            <div class="el-input">
                                                <input id="intent_score" type="text" autocomplete="off" maxlength="3" class="el-input__inner">
                                            </div>分
                                        </span>
                                    </div>
                                </div>
                                <div class="SubScene hiddenToVisible" style="display: none">
                                    <span class="title-left">所属语境：</span>
                                    <div class="el-select">
                                        <div class="el-input el-input--suffix" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            <input id="-scene_word" type="text" autocomplete="off" placeholder="请选择" readonly="readonly" class="el-input__inner" data-toggle="dropdown">
                                            <span class="el-input__suffix">
                                                <span class="caret"></span>
                                            </span>
                                            <ul id="-scene_word_list"  class="dropdown-menu">
                                                <li><a href="#">Action</a></li>
                                            </ul>
                                        </div>
                                    </div>
                                    <p>该节点当前是属于哪个语境</p>
                                    <button  id="senceSetting" type="button" class="el-button el-button--primary">
                                        <span>语境设置</span>
                                    </button>
                                </div>
                                <img src="../assets/public/img/asr-audio/reflect1.png" class="reflect">
                            </div>

                            <!--关键词内容,关键词有效范围-->
                            <div class="mBox hiddenToVisible" style="display: none">
                                <div class="line"></div>
                                <div class="idenKeyWords">
                                    <span class="title-left">关键词内容：</span>
                                    <div class="el-textarea">
                                        <textarea  id="-keywords"  maxlength="1000" placeholder="多个关键词用英文的“;”分割,例如：你是;你是谁" class="el-textarea__inner" style="min-height: 31px;">
                                        </textarea>
                                    </div>
                                </div>
                                <div class="SubScene hiddenToVisible">
                                    <span class="title-left">关键词有效范围：</span>
                                    <div class="el-select">
                                        <div class="el-input el-input--suffix" data-toggle="dropdown"
                                             aria-haspopup="true" aria-expanded="false">
                                            <input id="-keywords_scope" type="text" autocomplete="off" placeholder="请选择" readonly="readonly" class="el-input__inner" data-toggle="dropdown">
                                            <span class="el-input__suffix">
                                                <span class="caret"></span>
                                            </span>
                                            <ul id="-keywords_scope_list" class="dropdown-menu">
                                                <li><a href="#">Action</a></li>
                                            </ul>
                                        </div>
                                    </div>
                                    <p style="width: 100%; margin-left: 114px; color: rgb(230, 162, 60);">
                                        当前关键词内容，在“全部”语境，客户说中均有效
                                    </p>
                                </div>
                                <img src="../assets/public/img/asr-audio/reflect2.png" class="reflect">
                            </div>


                            <!--设置语音内容,添加语音内容 文件上传-->
                            <div class="mBox">
                                <div class="line">
                                </div>
                                <div class="flowText">
                                    <div id="files_content" class="flowContents" >
                                    </div>
                                    <div class="addAudio">
                                        <span class="title-left">添加语音内容：</span>
                                        <div class="addAudioBtn" id="addFile">
                                            +
                                        </div>
                                    </div>
                                </div>
                                <img src="../assets/public/img/asr-audio/reflect3.png" class="reflect">
                            </div>

                            <!--设置识别意向,添加识别意向-->
                            <div class="mBox" id="intents_step">
                                <div class="line"></div>
                                <div class="discern"  style="float: left">
                                    <span class="title-left" style="float: left;height: 100px;display:inline;">
                                        <div style="height: 50px;line-height:50px;vertical-align: middle;">识别意向词语  :</div>
                                        <div style="height: 50px;line-height:50px;vertical-align: middle;">识别意向词语  :</div>
                                    </span>
                                    <div style="float: left;overflow-x: auto;width: 600px;height: 110px;padding:0px 8px "   id="">
                                        <div style="float: left;height: 100px;width:850px;" id="intents_content">
                                        </div>
                                    </div>
                                </div>
                                <!--设置语音内容,添加语音内容 文件上传-->
                                <div class="flowText">
                                    <div id="-addIntent" class="flowContents" >
                                    </div>
                                    <div class="addAudio">
                                        <span class="title-left">添加识别意向：</span>
                                        <div class="addAudioBtn" id="addIntent">
                                            +
                                        </div>
                                    </div>
                                </div>
                                <img src="../assets/public/img/asr-audio/reflect4.png" class="reflect">
                            </div>


                            <!--reflect-->
                            <div class="mBox"> <img src="../assets/public/img/asr-audio/reflect5.png" class="reflect"></div>

                            <!--reflect-->
                            <div class="mBox"> <img src="../assets/public/img/asr-audio/reflect6.png" class="reflect"></div>

                            <!--保存与删除节点的按钮-->
                            <div class="flowBtns">
                                <button type="button" id="save_lc" class="el-button flowBtnSure el-button--primary"><span>保存</span></button>
                                <button type="button" id="delete_lc" class="el-button flowBtnCancel el-button--primary"><span>删除节点</span></button>
                            </div>

                        </div>
                    </div>

                    <!--掩盖层-->
                    <div class="el-loading-mask" style="display: none;">
                        <div class="el-loading-spinner">
                            <svg viewBox="25 25 50 50" class="circular">
                                <circle cx="50" cy="50" r="20" fill="none" class="path"></circle>
                            </svg>
                        </div>
                    </div>
                </div>


                <!--隐藏的窗口弹出窗1-->
                <div class="windowPop ployKeywords" id="keyword_intent">
                    <div class="increased-form">
                        <div class="increased-title">
                            <span></span>
                            <i></i>
                        </div>
                        <div class="increased-body">
                            <div class="PopBox1">
                                <div class="el-textarea">
                                    <textarea rows="2" id="keyword_intent_content" placeholder="请输入内容" maxlength="1000" class="el-textarea__inner" style="min-height: 31px;">
                                    </textarea>
                                </div>
                                <label role="checkbox" class="el-checkbox">
                                    <span aria-checked="mixed" class="el-checkbox__input">
                                        <span class="el-checkbox__inner"></span>
                                        <input type="checkbox"  aria-hidden="true" class="el-checkbox__original" value="">
                                    </span>
                                    <span class="el-checkbox__label" style="display: none">修改所有的</span>
                                </label>
                                <div class="flowBtns">
                                    <button type="button" id="save_keyword_intent" class="el-button el-button--primary"><span>保存并退出</span></button>
                                    <button type="button" id="cancel_keyword_intent" class="el-button el-button--default"><span>取消</span></button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>


                <!--隐藏的窗口弹出窗2 请选择跳转话术节点-->
                <div class="windowPop continue" id="continue">
                    <div class="increased-form">
                        <div class="increased-title">
                            <span>请选择跳转话术节点</span><i></i>
                        </div>
                        <div id="next_intent">
                            <div class="increased-body">
                                <div class="continueTitle">
                                    <ul class="hiddenToVisible">
                                        <li class="hiddenToVisible">
                                        <span class="title-left" style="height: 40px;line-height:40px;vertical-align: middle;">
                                            话术类型：
                                        </span>
                                            <div class="el-select" style="height: 40px;line-height:40px;vertical-align: middle;">
                                                <select id="asrkeyword-keyword_id" name="AsrKeyword[keyword_id]" value="0" class="form-control">
                                                    <option value="0" selected="selected">主流程</option>
                                                    <option value="1">业务流程</option>
                                                    <option value="2">系统流程</option>
                                                    <option value="3">结束流程</option>
                                                </select>
                                            </div>
                                        </li>
                                        <li class="hiddenToVisible">
                                        <span class="title-left" style="height: 40px;line-height:40px;vertical-align: middle;">
                                            话术节点：
                                        </span>
                                            <div class="el-select" style="height: 40px;line-height:40px;vertical-align: middle;">
                                                <select id="asrkeywordclassify-id" name="AsrKeyword[keyword_id]" value="0" class="form-control">
                                                </select>
                                            </div>
                                        </li>
                                    </ul>
                                </div>
                                <div class="flowContentBox flowBox" id="fileContents">
                                    <div class="flowContent">
                                        <span class="title-left">语音内容：</span>
                                        <div class="el-textarea is-disabled">
                                            <textarea disabled="disabled" rows="5" class="el-textarea__inner" style="min-height: 31px;"></textarea>
                                        </div>
                                        <div class="audios">
                                            <div class="audio">
                                                <audio controls="controls" src="" class="voice"></audio>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="flowBtns">
                                    <button type="button" id="save_next_intent" class="el-button el-button--primary"><span>保存</span>
                                    </button>
                                    <button type="button" id="flush_next_intent" class="el-button el-button--warning"><span>清空</span>
                                    </button>
                                    <button type="button" id="cancel_next_intent"  class="el-button nextCancel el-button--primary">
                                        <span>取消</span>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>


                <!--隐藏的窗口弹出窗3 请选择跳转话术节点-->
                <div class="windowPop SceneSet" id="scene-set">
                    <div class="increased-form">
                        <div class="increased-title">
                            <span>语境设置</span>
                            <i></i>
                        </div>
                        <div class="increased-body">
                            <div class="SceneBox">
                                <div class="table SceneTable">
                                    <div class="el-table el-table--fit el-table--border el-table--scrollable-x el-table--enable-row-hover"
                                         style="width: 100%;">
                                        <div class="hidden-columns">
                                            <div></div>
                                            <div></div>
                                        </div>
                                        <div class="el-table__header-wrapper">
                                            <table cellspacing="0" cellpadding="0" border="0" class="el-table__header"
                                                   style="width: 290px;">
                                                <colgroup>
                                                    <col name="el-table_1_column_1" width="210">
                                                    <col name="el-table_1_column_2" width="80">
                                                    <col name="gutter" width="0">
                                                </colgroup>
                                                <thead class="has-gutter">
                                                <tr class="">
                                                    <th colspan="1" rowspan="1" class="el-table_1_column_1     is-leaf">
                                                        <div class="cell">语境</div>
                                                    </th>
                                                    <th colspan="1" rowspan="1" class="el-table_1_column_2     is-leaf">
                                                        <div class="cell">操作</div>
                                                    </th>
                                                    <th class="gutter" style="width: 0px; display: none;"></th>
                                                </tr>
                                                </thead>
                                            </table>
                                        </div>
                                        <div class="el-table__body-wrapper is-scrolling-left">
                                            <table cellspacing="0" cellpadding="0" border="0" class="el-table__body"
                                                   style="width: 290px;">
                                                <colgroup>
                                                    <col name="el-table_1_column_1" width="210">
                                                    <col name="el-table_1_column_2" width="80">
                                                </colgroup>
                                                <tbody></tbody>
                                            </table>
                                            <div class="el-table__empty-block" style="width: 290px;"><span
                                                        class="el-table__empty-text">暂无数据</span></div></div>

                                        <div class="el-table__column-resize-proxy" style="display: none;"></div>
                                    </div>
                                </div>
                                <div class="addScen"><p>+</p></div>
                            </div>
                            <div class="flowBtns">
                                <button type="button" class="el-button el-button--primary">
                                    <span>确定</span>
                                </button>
                                <button type="button" class="el-button outScenewords el-button--primary">
                                    <span>取消</span>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>

                <!--隐藏的窗口弹出窗4 新增intent-->
                <div class="windowPop SceneSet" id="add-intent">
                    <div class="increased-form">
                        <div class="increased-title">
                            <span>新增意向</span>
                            <i></i>
                        </div>
                        <div class="increased-body">
                            <div class="PopBox1">
                                <div class="el-textarea">
                                    <textarea rows="1" name="intent_name" placeholder="请输入意向名称" maxlength="8" class="el-textarea__inner" style="min-height: 11px;">
                                    </textarea>
                                </div>
                            </div>
                            <br>
                            <div class="el-textarea">
                                <textarea rows="2"  name="intent_content"  placeholder="请输入意向关键字，用;隔开" maxlength="10000" class="el-textarea__inner" style="min-height: 31px;">
                                </textarea>
                            </div>
                            <div class="flowBtns">
                                <button type="button" class="el-button el-button--primary"><span>保存并退出</span></button>
                                <button type="button" class="el-button el-button--default"><span>取消</span></button>
                            </div>
                        </div>
                    </div>
                </div>

            </div>

        </div>
    </div>
</div>


<script src="../assets/public/js/asr-audio/deal-flow.js"></script>

<!--自己寫的js m 代表模型 v 代表视图 c 代表控制器-->
<script>
    //切换导航
    $('#navbar-collapse ul li').unbind('click').click(function () {
        $('#navbar-collapse ul li').removeClass('active');
        $(this).addClass('active');
        var href = $(this).children()[0].href
        console.log($(this).children()[0].href);
        location.href = href;
    });
    //导航栏的鼠标经过事件
    $(".main-header li").hover(function () {//导航栏的鼠标经过事件
        $(this).addClass("active");
    },function () {
        $(this).removeClass("active");
    });
    //把流程id保存在js全局变量里面
    ad.lcid = "<?php echo $lc_id?>";//业务流程id
    //重新将$赋给一个全局变量window.temp_jQyery = $，这样点击事件里面再调用的时候用$ = window.temp_jQyery;就可以实现将其传递到click事件的作用域里面
    window.temp_jQyery = $;
    ad.url = "<?= Yii::$app->homeUrl ?>";//为网站路径
    //更新页面的内容
    refreshContent();
    $("#save_lc").bind('click',function () {
        var name = $("#name").val();
        var intent_score = $("#intent_score").val();
        var keywords = $("#-keywords").val();
        $ = window.temp_jQyery;
        $.apiPost("save-lc?id="+ad.lcid,
            {name:name,intent_score:intent_score,keywords:keywords},
            function (d) {
                if (d && d.Result) {
                    if (d.Data) {
                        refreshContent();
                        layer.msg('修改成功！');
                    }
                } else if (d && d.Message) {
                    layer.msg(d.Message);
                } else {
                    layer.msg('修改失败！');
                }
            });
    });
    $("#delete_lc").bind('click',function () {
        $ = window.temp_jQyery;
        if (confirm("确认是否删除该节点？")){
            $.apiPost("delete-lc?id="+ad.lcid,
                {},
                function (d) {
                    if (d && d.Result) {
                        if (d.Data) {
                            layer.msg('删除节点成功！');
                            location.reload();
                        }
                    } else if (d && d.Message) {
                        layer.msg(d.Message);
                    } else {
                        layer.msg('删除节点失败！');
                    }
                });
        }
    });
    //新增父节点
    $("#add_parent").unbind('click').bind('click',function () {
        $ = window.temp_jQyery;
        addNode($(this).index(".sonList ul"),0);
    });

    //更新整个页面的内容
    function refreshContent() {
        //更新左边的内容
        findNodeList();
    }

    /** 节点操作 */
    //查询所有的节点列表，并且将节点显示到前台页面 m
    function findNodeList(){
        $ = window.temp_jQyery;
        $.apiPost("find-node-list?id="+ad.audio_id,
            {},
            function (d) {
                if (d && d.Result) {
                    if (d.Data) {
                        ad.MainForm.NodeList = d.Data;
                        refreshNodeList(d.Data);
                        //更新其点击事件
                        addNodeListEvent();
                        //更新右边的内容
                        findLc();
                        //更新右边的内容
                        findFileList();
                        //更新右边的意图列表
                        findIntentList();
                    }
                } else if (d && d.Message) {
                    layer.msg(d.Message);
                } else {
                    layer.msg('查询节点失败！');
                }
            });
    }

    //传入节点的所有列表数据 为数组 v
    function refreshNodeList(item){
        $(".sonList").html("");
        var index = 0;
        var html = "";
        for (var i = 0;i<item.length;i++){
            var v = item[i];//当前元素
            if (v['level'] == 1){
                html += '</ul>';
                html += '<ul class="son2 scene'+index+'">';
                html += '<li class="businessTit"><p>'+v['name']+'<em class="triangle"></em></p></li>';
                index ++;
            }else{
                if (v['id']==ad.lcid){
                    html += '<li parent_id="'+v['parent_id']+'" lc_id="'+v['id']+'" style="color:#4fa2fe" ><div class="sTit"><p class="">'+v['name']+'</p> <span title="添加" class="hBox"><img src="../assets/public/img/asr-audio/answer_add.png"></span></div></li>';
                }else{
                    html += '<li parent_id="'+v['parent_id']+'" lc_id="'+v['id']+'" ><div class="sTit"><p class="">'+v['name']+'</p> <span title="添加" class="hBox"><img src="../assets/public/img/asr-audio/answer_add.png"></span></div></li>';
                }
            }
        }
        $(".sonList").html(html);
        $("[lc_id='"+ad.lcid+"']").parents(".son2").addClass("open");
    }

    //开启列表的点击添加事件 c
    function addNodeListEvent(){
        //展开
        $('.businessTit').unbind('click').bind('click',function () {
            ad.classOpen($(this).parent().index(".sonList ul"));
        });
        //点击每个li
        $('.sTit p').unbind('click').bind('click',function () {
            $('.sTit p').each(function () {
                $(this).attr('style','color:#333');
            });
            $(this).attr('style','color:#4fa2fe');
            var lc_id = parseInt($(this).parent().parent().attr("lc_id"));
            ad.lcid = lc_id;
            refreshContent();
        });
        /**
         * 点击添加按钮添加节点
         * */
        $(".hBox").unbind('click').bind('click',function () {
            $ = window.temp_jQyery;
            var ul_n = $(this).parents(".sonList ul").index();
            var lc_id = parseInt($(this).parent().parent().attr("lc_id"));
            addNode($(this).parents(".sonList .scene"+ul_n+" li").index(),$(this).parent().parent().attr("parent_id"),$(this) );
            refreshContent();
        });
    }

    //新增节点
    function addNode(index,parent_id){
        $.apiPost("add-node",
            {audio_id:ad.audio_id,parent_id:parent_id},
            function (d) {
                if (d && d.Result) {
                    if (d.Data) {
                        ad.lcid = d.Data.id;
                        findNodeList();
                    }else{
                    }
                    layer.msg('添加节点成功！');
                } else if (d && d.Message) {
                    layer.msg(d.Message);
                } else {
                    layer.msg('添加节点失败！');
                }
            });
    }

    /** 流程操作 */
    //查询流程 m
    function findLc(){
        $ = window.temp_jQyery;
        $.apiPost("find-lc?id="+ad.lcid,
            {},
            function (d) {
                if (d && d.Result) {
                    if (d.Data) {
                        ad.MainForm.Lc = d.Data;
                        refreshLc(d.Data);
                    }
                } else if (d && d.Message) {
                    layer.msg(d.Message);
                } else {
                    layer.msg('查询流程数据失败！');
                }
            });
    }

    //传入流程对象更新数据 v
    function refreshLc(item){
        var name = $("#name");
        var intent_score = $("#intent_score");
        name.attr("value",item.name);
        intent_score.attr("value",item.intent_score);
        name.val(item.name);
        intent_score.val(item.intent_score);
    }

    /** 文件列表操作 */
    //查询文件列表操作 m
    function findFileList(){
        $ = window.temp_jQyery;
        $.apiPost("find-file-list?id="+ad.lcid,
            {},
            function (d) {
                if (d && d.Result) {
                    if (d.Data) {
                        ad.MainForm.FileList = d.Data;
                        refreshFileList(d.Data);
                        //新增文件操作相关的事件
                        addFileEvent();
                    }
                } else if (d && d.Message) {
                    layer.msg(d.Message);
                } else {
                    layer.msg('查询文件列表失败！');
                }
            });
    }

    // 刷新文件列表的界面 v
    function refreshFileList(item){
        $("#files_content").html('');
        for(var i=0;i<item.length;i++){
            //html转为html的内容
            var v1 = (i+1);
            var v2 = item[i].content;
            var v3 = item[i].filepath;
            var v4 = item[i].files_id;
            var flowContent =
                [
                    '                                        <div class="flowContent" id="files_content'+v1+'" index="'+v1+'" files_id="'+v4+'">',
                    '				                            <span class="title-left">设置语音内容：<br>',
                    '                                                （第'+v1+'条）',
                    '                                            </span>',
                    '                                            <div class="el-textarea">',
                    '                                                <textarea id="-sound-content'+v1+'" maxlength="1000" rows="5" class="el-textarea__inner" style="min-height: 31px;">'+v2+'</textarea>',
                    '                                            </div>',
                    '                                            <div class="audios">',
                    '                                                <div class="audio">',
                    '                                                    <audio id="-sound-filepath'+v1+'" controls="controls" src="..'+v3+'" class="voice"></audio>',
                    '                                                </div>',
                    '                                                <button type="button" class="el-button btnLeft el-button--primary">',
                    '                                                    <span><img src="../assets/public/img/asr-audio/turnText.png"></span>',
                    '                                                </button>',
                    '                                                <div class="btnRight">',
                    '                                                    <button type="button" class="el-button r_btn el-button--primary delete_sound delete_file">',
                    '                                                        <span>删除语音</span>',
                    '                                                    </button>',
                    '                                                    <div class="upload-demo">',
                    '                                                        <div tabindex="0" class="el-upload el-upload--text">',
                    '                                                           <form method="post" enctype="multipart/form-data">',
                    '                                                               <button  id="upload_button'+v1+'" type="button" class="el-button el-button--primary el-button--small">',
                    '                                                                   <span><img src="../assets/public/img/asr-audio/shangc.png">上传语音</span>',
                    '                                                               </button>',
                    '                                                               <input id="upload_input'+v1+'" type="file" name="file" accept=".wav,.mp3" value="" class="el-upload__input">',
                    '                                                           </form>',
                    '                                                        </div>',
                    '                                                    </div>',
                    '                                                </div>',
                    '                                            </div>',
                    '                                            <i></i>',
                    '                                        </div>'
                ].join("");
            $("#files_content").append(flowContent);//将文件的内容以及路径加到后面
            //文件上传的配置项
        }
    }

    //新增文件操作相关的事件 c
    function addFileEvent(){
        var options = {
            allowedExtensions: "wav",
            invalidExtError: "无效的文件类型",
            maxSize: 0,
            sizeError: "文件太大",
            showPreview: true,
            showFilename: true,
            showPercent: true,
            showErrorAlerts: true,
            errorOnResponse: "上传文件出错",
            onSubmit: false,
            url: "uploadfile",
            data: null,
            limit: 1,
            limitError: "上传文件出错",
            delfiletext: "移除",
            onFileError: function(file, error) {//文件上传失败后
                layer.msg('上传文件失败！');
            },
            onFileSuccess: function(file, data) {//文件上传成功后
                findFileList();
                layer.msg('上传文件成功！');
            }
        };
        //文件上传的事件
        $(".flowContent form button").unbind('click').bind('click',function () {
            $ = window.temp_jQyery;
            var index = parseInt($(this).parents(".flowContent").attr("index"));
            //移除掉input
            $("#upload_input"+index).remove();
            //添加input
            $("#upload_button"+index).after(
                '<input id="upload_input'+index+'" type="file" name="file" accept=".wav,.mp3" value="" class="el-upload__input" >'
            );
            $("#upload_input"+index).change(function () {
                $ = window.temp_jQyery;
                //文件上传之前先进行验证文件格式以及文件大小等信息
                $result = ad.beforeAvatarUpload(this.files[0]);
                if($result){//验证成功
                    //点击就自动修改文件的ID，内容等信息
                    options.url = "uploadfile?files_id="+$("#files_content"+index).attr("files_id")+"&content="+$("#-sound-content"+index).val()+"&audio_id="+ad.audio_id+"&lc_id="+ad.lcid;
                    $("#upload_input"+index).pekeUpload(options);
                }
            });
            $("#upload_input"+index).click();
        });
        //文件删除的事件
        $(".delete_file").unbind('click').bind('click',function () {
            $ = window.temp_jQyery;
            var index = parseInt($(this).parents(".flowContent").attr("index"));
            deleteFile($("#files_content"+index).attr("files_id"));
        });
        //新增文件点击事件
        $('#addFile').unbind('click').bind('click',function () {
            addFile();
        })
    }

    //删除文件，传入文件的id
    function deleteFile(files_id) {
        ad.$confirm('此操作将删除该录音, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            //走数据库删除该文件，然后删除该文件的关联的file_id里面的id
            $.apiPost("delete-files?files_id=" + files_id+"&audio_id="+ad.audio_id+"&lc_id="+ad.lcid,
                {data:'data'},
                function (d) {
                    if (d && d.Result) {
                        //刷新文件列表
                        findFileList();
                        ad.$message({type: 'success', message: '删除成功!'});
                    } else if (d && d.Message) {
                        layer.msg(d.Message);
                    } else {
                        layer.msg('删除失败！');
                    }
                });

        }).catch(() => {
            ad.$message({
                type: 'info',
                message: '已取消删除'
            });
        });
    }

    //添加语音文件
    function addFile(){
        ad.MainForm.FileList.push({content: '', filepath: "",files_id: '', });
        refreshFileList(ad.MainForm.FileList);
        //新增文件操作相关的事件
        addFileEvent();
        setTimeout(() => {
                $('.flowContents').scrollTop(1000000);
                $('.flowContents .el-textarea__inner').eq(this.MainForm.AnswerSounds.length-1).select();
            },
            100);
    }

    /** 识别意向操作 */
    // m
    function findIntentList(){
        $ = window.temp_jQyery;
        $.apiPost("find-intent-list?id="+ad.lcid,
            {},
            function (d) {
                if (d && d.Result) {
                    if (d.Data) {
                        ad.MainForm.Intents = d.Data;
                        refreshIntentList(d.Data);
                        //新增操作相关的事件
                        addIntentEvent();
                        addNextKeywordIntentEvent();
                        addNextIntentEvent();
                        addDeleteIntentEvent();
                    }
                } else if (d && d.Message) {
                    layer.msg(d.Message);
                } else {
                    layer.msg('查询文件列表失败！');
                }
            });
    }

    // v
    function refreshIntentList(){
        $("#intents_content").html('');
        if (ad.MainForm.Intents.length<1){
            $("#intents_step .discern").css("display","none");
        }else{
            $("#intents_step .discern").css("display","block");
        }
        for(var i=0;i<ad.MainForm.Intents.length;i++){
            var intent = ad.MainForm.Intents[i];
            //html转为html的内容
            var v1 = (i+1);
            var v2 = intent.keyword_id;
            var v3 = intent.intent_name;
            var v4 = intent.content;
            var intentsContent =
                [
                    '<div style="width: 150px;padding:0px;margin: 0px;float: left;overflow:hidden;position:relative" id="intentContent_'+v2+'" index="'+v1+'" my-id="'+v2+'">',
                    '<image id="intent_delete_'+v2+'"  src="../assets/public/img/delete.png" title="删除该条记录" style="z-index=9999;cursor:pointer;width: 15px;display:none;height:15px;margin-left:130px;position:absolute;"/>',
                    '<div style="height:50px;line-height:50px;vertical-align: middle;">',
                    '<button type="button"  my-id="'+v2+'" myname="'+v3+'"  id="intent_content_'+v2+'" class="el-button letterBtn affrim el-button--primary"><span>'+v3+'</span></button>',
                    '</div>',
                    '<div style="height:50px;line-height:50px;vertical-align: middle;">',
                    '<button type="button"  my-id="'+v2+'" myname="'+v3+'" id="next_step_'+v2+'" class="el-button letterBtn nextStep el-button--primary"><span>下一步</span></button>',
                    '</div>',
                    '</div>'
                ].join("");
            $("#intents_content").append(intentsContent);//将文件的内容以及路径加到后面
        }
        $("#intents_content").css("width",ad.MainForm.Intents.length*150);
    }

    //新增意图的点击事件 添加识别意向 等  c1
    function addIntentEvent() {
        // 添加意向的点击事件，
        $("#addIntent").unbind('click').bind('click', function () {//根据意向跳转的下一步按钮点击事件
            $ = window.temp_jQyery;
            $("#add-intent").css('display', 'block');
            $(".continue .increased-title span").html("请选择检测到“" + $(this).attr("myname") + "”后跳转到的话术节点");
        });
        //保存按钮点击后
        $("#add-intent .el-button--primary").unbind('click').click(function () {//上面两种的取消
            $ = window.temp_jQyery;
            //检测意图名字是否为空
            var intent_name = $("#add-intent [name='intent_name']").val();
            if (intent_name == "") {
                alert("请输入意图的名称！");
                $("#add-intent [name='intent_name']").focus();
                return;
            };
            $("#add-intent").css('display', 'none');
            addIntent();
        });
        //取消按钮点击后
        $("#add-intent .el-button--default").unbind('click').click(function () {//上面两种的取消
            $ = window.temp_jQyery;
            $("#add-intent").css('display', 'none');
        });
    }

    //添加 意图
    function addIntent(){
        //走数据库新增一个意图
        $.apiPost("add-intent",
            {
                keyword_id: 0,
                intent_name: $("#add-intent [name='intent_name']").val(),
                content: $("#add-intent [name='intent_content']").val(),
                classify_id: ad.lcid,
                classify_type: 2
            },
            function (d) {
                if (d && d.Result) {
                    //刷新文件列表
                    findIntentList();
                    ad.$message({type: 'success', message: '新增意向成功!'});
                } else if (d && d.Message) {
                    layer.msg(d.Message);
                } else {
                    layer.msg('新增意向失败！');
                }
            });
    }

    //新增下一步的 识别意向词语 c2
    function addNextKeywordIntentEvent(){
        // 肯定以及拒绝的词汇的选择以及取消
        $("[id^='intent_content_']").unbind('click').bind('click',function () {//肯定以及拒绝的词汇
            $(".ployKeywords").css('display','block');
            var id = $(this).attr("my-id");
            ad.keword_id = parseInt(id);
            $(".ployKeywords .increased-title span").html($(this).attr("myname"));
            findNextKeywordIntent();
        })
        //肯定拒绝词语的取消
        $("#cancel_keyword_intent").unbind('click').click(function () {
            $(".ployKeywords").css('display','none');
        });
        //保存
        $("#save_keyword_intent").unbind('click').click(function () {
            $ = window.temp_jQyery;
            var keyword_intent_content = $("#keyword_intent_content").val();
            $.apiPost("save-keyword-intent?id="+ad.keword_id,
                {content:keyword_intent_content},
                function (d) {
                    if (d && d.Result) {
                        ad.$message({type: 'success', message: '修改成功!'});
                        $("#keyword_intent").css("display","none");
                    } else if (d && d.Message) {
                        layer.msg(d.Message);
                    } else {
                        layer.msg('修改失败！');
                    }
                });
        });
    }

    // m
    function findNextKeywordIntent(){
        $ = window.temp_jQyery;
        $.apiPost("find-next-keyword-intent?id="+ad.keword_id,
            {},
            function (d) {
                if (d && d.Result) {
                    refreshNextKeywordIntent(d.Data);
                } else if (d && d.Message) {
                    layer.msg(d.Message);
                } else {
                    layer.msg('查询失败！');
                }
            });
    }
    // v
    function refreshNextKeywordIntent(item) {
        $("#keyword_intent_content").val(item.content);
    }

    //新增下一步的 识别意向节点 c3 点击事件
    function addNextIntentEvent(){
        // 下一步的点击事件，根据意向跳转
        $("[id^='next_step_']").unbind('click').bind('click',function () {//根据意向跳转的下一步按钮点击事件
            var id = $(this).attr("my-id");
            $("#continue").css('display','block');
            ad.next_intent_id = id;
            findNextIntent1();
            $("#continue .increased-title span").html("请选择检测到“"+$(this).attr("myname")+"”后跳转到的话术节点");
            $("#continue").attr("my-id",id);
        });
        //取消
        $("#cancel_next_intent").unbind('click').click(function () {
            $("#continue").css("display","none");
        });
        //请选择跳转话术节点的点击取消事件
        $(".flowBtns .nextCancel").unbind('click').click(function () {//上面两种的取消
            $(".continue").css('display','none');
        });
        //清空的事件
        $("#flush_next_intent").unbind('click').bind('click',function () {
            $ = window.temp_jQyery;
            ad.next_classify_type = 0;
            ad.next_classify_id = 0;
            findNextIntent1();
        });
        //保存的事件
        $("#save_next_intent").unbind('click').bind('click',function () {
            $ = window.temp_jQyery;
            $.apiPost("save-next-intent?id="+ad.next_intent_id+"&next_classify_type=" + ad.next_classify_type + "&next_classify_id=" +ad.next_classify_id,
                {data:'data'},
                function (d) {
                    if (d && d.Result) {
                        ad.$message({type: 'success', message: '修改成功!'});
                        $("#continue").css("display","none");
                    } else if (d && d.Message) {
                        layer.msg(d.Message);
                    } else {
                        layer.msg('修改失败！');
                    }
                });
        });
    }

    //新增下一步的 识别意向节点 c3 m1
    function findNextIntent1(){
        $ = window.temp_jQyery;
        $.apiPost("find-next-intent?id=" + ad.next_intent_id,
            {},
            function (d) {
                if (d && d.Result) {
                    var next_classify_type = d.Data.next_intent.next_classify_type;
                    if (next_classify_type == null){
                        next_classify_type = 0;
                    }
                    var next_classify_id = d.Data.next_intent.next_classify_id;
                    if (next_classify_id == null){
                        next_classify_id = 0;
                    }
                    ad.next_classify_type = next_classify_type;
                    ad.next_classify_id = next_classify_id;
                    //刷新节点
                    refreshNextIntent1(d.Data);
                    addNextIntentEvent1();
                    findNextIntent2();
                }
            });
    }

    //新增下一步的 识别意向节点 c3 v1
    function refreshNextIntent1(){
        $ = window.temp_jQyery;
        $("#asrkeyword-keyword_id option[value='"+ad.next_classify_type+"']").attr("selected","selected");
    }

    // c3 c1
    function addNextIntentEvent1(){
        //点击话术类型后的点击事件
        $("#asrkeyword-keyword_id").change(function () {
            $ = window.temp_jQyery;
            var next_classify_type = $("#asrkeyword-keyword_id option:selected").attr("value");
            ad.next_classify_type = next_classify_type;
            findNextIntent2();
        });
    }

    //新增下一步的 识别意向节点 c3 m2
    function findNextIntent2() {
        $ = window.temp_jQyery;
        $.apiPost("find-all-intent?typeid=" + ad.next_classify_type,
            {},
            function (d) {
                if (d && d.Result) {
                    refreshNextIntent2(d.Data);
                    addNextIntent2Event();
                    findNextIntentFilelist();
                }
            });
    }

    //新增下一步的 识别意向节点 c3 v2
    function refreshNextIntent2(data){
        var all_intent = data.all_intent;
        //下一个节点的所有节点内容
        var all_classify_html = "";
        if (all_intent!=null){
            for (var i=0;i<all_intent.length;i++) {
                var lc_id = 0;
                var parent_id = -1;
                switch (""+ad.next_classify_type){
                    case "0":
                        lc_id = all_intent[i].keyword_classify_id;
                        parent_id = all_intent[i].parent_id;
                        break;
                        parent_id = all_intent[i].parent_id;
                    case "1":
                        lc_id = all_intent[i].id;
                        parent_id = all_intent[i].parent_id;
                        break;
                    case "2":
                        lc_id = all_intent[i].id;
                        parent_id = all_intent[i].parent_id;
                        break;
                    case "3":
                        lc_id = all_intent[i].keyword_classify_end_id;
                        parent_id = all_intent[i].parent_id;
                        break;
                }
                if(parent_id == 0){
                    all_classify_html += '<option value="'+lc_id+'" disabled="disabled">'+all_intent[i].name+'</option>';
                }else{
                    all_classify_html += '<option value="'+lc_id+'">&nbsp;&nbsp;'+all_intent[i].name+'</option>';
                }
            }
        }
        $("#asrkeywordclassify-id").html(all_classify_html);
        $("#asrkeywordclassify-id option[value='"+ad.next_classify_id+"']").attr("selected","selected");
    }

    //新增下一步的 识别意向节点 的点击事件 c3 c2
    function addNextIntent2Event(){
        //点击话术节点后的点击事件
        $("#asrkeywordclassify-id").change(function () {
            $ = window.temp_jQyery;
            var next_classify_id = $("#asrkeywordclassify-id option:selected").attr("value");
            ad.next_classify_id = next_classify_id;
            findNextIntentFilelist();
        });
    }

    //新增下一步的 识别意向 查找所有的文件 c3 m3
    function findNextIntentFilelist(item){
        $ = window.temp_jQyery;
        $.apiPost("find-intent-filelist?next_classify_type=" + ad.next_classify_type + "&next_classify_id=" +ad.next_classify_id,
            {},
            function (d) {
                if (d && d.Result) {
                    //刷新节点
                    refreshNextIntentFilelist(d.Data);
                }
            });
    }

    //新增下一步的 识别意向 查找所有的文件 c3 v3
    function refreshNextIntentFilelist(file_arr){
        //下一个节点的所有文件语音
        var files_html = "";
        if(file_arr != null){
            for (var i=0;i<file_arr.length;i++){
                var res =
                    [
                        '<div class="flowContent">',
                        '     <span class="title-left">语音内容：</span>',
                        '    <div class="el-textarea is-disabled">',
                        '       <textarea disabled="disabled" rows="5" class="el-textarea__inner" style="min-height: 31px;">'+file_arr[i].content+'</textarea>',
                        '     </div>',
                        '     <div class="audios">',
                        '         <div class="audio">',
                        '                 <audio controls="controls" src="'+ad.url+file_arr[i].filepath+'" class="voice"></audio>',
                        '             </div>',
                        '     </div>',
                        '</div>'
                    ].join("");
                files_html += res;
            }
        }
        $("#fileContents").html(files_html);
    }

    //c3 c3

    //新增 识别意向 删除事件 c4
    function addDeleteIntentEvent(){
        $("[id^='intentContent_']").hover(function () {
            var id = $(this).attr("my-id");
            $(this).children("img").css("display","block");
            $(this).children("img").bind('click',function () {
                deleteIntent(id);
            });
            $(this).children("img").hover(function () {
                $(this).css("height",18);
                $(this).css("width",18);
            },function () {
                $(this).css("height",15);
                $(this).css("width",15);
            })
        },function () {
            var id = $(this).attr("my-id");
            $("#intent_delete_"+id).css("display","none");
        });
    }

    //删除意图
    function deleteIntent(id) {
        $ = window.temp_jQyery;
        ad.$confirm('此操作将删除该意图, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            //走数据库删除该文件，然后删除该文件的关联的file_id里面的id
            $.apiPost("delete-intent?id=" + id,
                {},
                function (d) {
                    if (d && d.Result) {
                        //刷新意图列表
                        findIntentList();
                        ad.$message({type: 'success', message: '删除成功!'});
                    } else if (d && d.Message) {
                        layer.msg(d.Message);
                    } else {
                        layer.msg('删除失败！');
                    }
                });

        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            });
        });
    }


</script>